package com.leo.config;

import com.leo.annotation.ScanIgnore;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.io.IOException;

/**
 * @ClassName: ApplicationContextConfig
 * @Description: 注册bean配置类，类似我们的applicationContext.xml文件
 * @Scope类型： singleton 表示在spring容器中的单例，通过spring容器获得该bean时总是返回唯一的实例
 * prototype表示每次获得bean都会生成一个新的对象
 * request表示在一次http请求内有效（只适用于web应用）
 * session表示在一个用户会话内有效（只适用于web应用）
 * globalSession表示在全局会话内有效（只适用于web应用）
 * @Author: leo825
 * @Date: 2020-05-14 21:33
 * @Version: 1.0
 */
@Configuration
@ScanIgnore
public class ApplicationContextConfig extends WebMvcConfigurationSupport {
    /**
     * 加载配置文件，类似之前配置文件中的：
     * <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
     *       <property name="locations">  
     *            <list>  
     *                <value>classpath:*.properties</value>  
     *            </list>
     *       <property/>
     * </bean>  
         
     * <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
     *    <property name="properties" ref="configProperties"/>
     * </bean>
     *
     * @return
     */
    @Bean(name = "configProperties")
    public PropertiesFactoryBean propertiesFactoryBean(){
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("application.properties"));
        propertiesFactoryBean.setSingleton(true);
        System.out.println("propertiesFactoryBean注册完毕");
        return propertiesFactoryBean;
    }

    @Bean(name = "propertyConfiguer")
    public MyPropertyPlaceholder preferencesPlaceholderConfigurer(PropertiesFactoryBean configProperties) throws IOException {
        MyPropertyPlaceholder propertyConfiguer = new MyPropertyPlaceholder();
        propertyConfiguer.setProperties(configProperties.getObject());
        System.out.println("preferencesPlaceholderConfigurer注册完毕");
        return propertyConfiguer;
    }

    /**
     * 创建jdbcTemplate
     * @return
     */
    @Bean(name = "jdbcTemplate")//只要使用name = "driverManagerDataSource"就可以将注册的bean依赖过来
    public JdbcTemplate jdbcTemplate(DriverManagerDataSource driverManagerDataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(driverManagerDataSource);
        System.out.println("jdbcTemplate注册完毕");
        return jdbcTemplate;
    }

    /**
     * 创建数据源
     * @return
     */
    @Bean(name = "driverManagerDataSource")
    public DriverManagerDataSource driverManagerDataSource(MyPropertyPlaceholder propertyConfiguer) {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(MyPropertyPlaceholder.getProperty("datesource.driverClassName"));
        driverManagerDataSource.setUrl(MyPropertyPlaceholder.getProperty("datesource.url"));
        driverManagerDataSource.setUsername(MyPropertyPlaceholder.getProperty("datesource.username"));
        driverManagerDataSource.setPassword(MyPropertyPlaceholder.getProperty("datesource.password"));
        System.out.println("driverManagerDataSource注册完毕");
        return driverManagerDataSource;
    }
}
